{-# OPTIONS -Wall #-}

--------------------------------------------------------------------------------
-- |
-- Module      :  Majalan.Symbolic.SimpleNoteList
-- Copyright   :  (c) Stephen Tetley 2014
-- License     :  BSD3
--
-- Maintainer  :  stephen.tetley@gmail.com
-- Stability   :  unstable
-- Portability :  GHC
--
-- Simple NoteList
--
--------------------------------------------------------------------------------

module Majalan.Symbolic.SimpleNoteList
  ( 

    Note(..)
  , GenEvent
  , Phrase
  , phrase
  , unphrase

  ) where

import Majalan.Pitch.ET12

import Majalan.Base
import Majalan.Base.Internal.Utils

import Data.Monoid


-- | Just notes, no rests, no variation in duration.
--
data Note = N ET12  
  deriving (Eq,Ord,Show)


-- Pitch * Duration * Amplitude
type GenEvent uctx impl = Decimal -> Decimal -> Event uctx impl
-- 

-- Phrase should have an abstract start time like Advance.
-- Unlike Advance the start time should be ScorePos (not abs time)

-- Must concatenate!

-- Think PostScript - in scores, phrases should look like strings.

newtype Phrase = Phrase { getPhrase :: H Note }


instance Show Phrase where
  show = show . toListH . getPhrase

instance Monoid Phrase where
  mempty = Phrase emptyH
  a `mappend` b = Phrase $ getPhrase a `appendH` getPhrase b

phrase :: [ET12] -> Phrase 
phrase = Phrase . fromListH . map N



unphrase :: GenEvent uctx impl -> Phrase -> Advance uctx ()
unphrase mf xs = quarter_note_length >>= \drn ->
               mapM_ (fn drn) (toListH $ getPhrase xs)
  where
    fn drn (N p) = let amp = 0.9
                       pch = pitchInHertz p
                   in aevent drn (mf pch amp)


pitchInHertz :: TPitch pch => pch -> Decimal
pitchInHertz pch = toHertz 440.0 pch

